# -*- coding: utf-8 -*-
# created on 2017/1/16
#

from mathsolver.functions.base import *
from sympy import Abs, Mul, symbols


class VectorEqUpdate(BaseFunction):
    def solver(self, *args):
        vectoreqs = sympify(args[0].value)
        if isinstance(vectoreqs, list):
            answer = {'Abs': [], 'Arith': []}
            for eq in vectoreqs:
                # 参数是一个向量
                if str(eq[0].func) == 'vector':
                    answer[eq[0]] = eq[1]
                # 参数是模长
                elif isinstance(eq[0], Abs):
                    if len(eq) == 2:
                        answer['Abs'].append([eq[0], eq[1]])
                    elif len(eq) == 3:
                        answer['Abs'].append([eq[0], eq[2]])
                        answer['Abs'].append([eq[1], eq[2]])
                # 参数是向量的四则运算
                elif isinstance(eq[0], Mul):
                    answer['Arith'].append([eq[0], eq[1]])
            self.output.append(BaseSymbolValue(answer))

        else:
            self.output.append(BaseSymbolValue(vectoreqs))
        return self


class VectorsInterAngle(BaseFunction):
    def solver(self, *args):
        vector_a = sympify(args[0].value)
        vector_b = sympify(args[1].value)
        du = sympify(args[2].value)
        du = du.args[0]
        angle = symbols('angle')
        answer = {angle: [vector_a, vector_b, du]}
        self.output.append(BaseSymbolValue(answer))
        return self


class WhatTriangle(BaseFunction):
    def solver(self, *args):
        triangle = sympify(args[0].name)
        answer = {'tri': [triangle]}
        self.output.append(BaseSymbolValue(answer))
        return self


class TriangleEqs(BaseFunction):
    def solver(self, *args):
        _input = args[1].sympify()
        answer = {'sides': [], 'angle': [], 'Abs': []}
        for item in _input:
            if str(item[0]) in ['a', 'b', 'c', 'line(AB)', 'line(BA)', 'line(AC)', 'line(CA)', 'line(BC)', 'line(CB)']:
                answer['sides'].append([item[0], item[1]])
            elif str(item[0]) in ['A', 'B', 'C']:
                if str(item[1].func) == 'd':
                    answer['angle'].append([str(item[0]), item[1].args[0]])
                else:
                    answer['angle'].append([str(item[0]), item[1]])
            elif str(item[0].func) == 'Angle':
                if len(str(item[0].args[0])) == 1:
                    k = item[0].args[0]
                else:
                    k = sympify(str(item[0].args[0])[1])
                if str(item[1].func) == 'd':
                    v = item[1].args[0]
                else:
                    v = item[1]
                answer['angle'].append([str(k), v])
            elif isinstance(item[0], Abs):
                answer['Abs'] = item[1]

        self.output.append(BaseSymbolValue(answer))
        return self


class TriangleVectorEqs(BaseFunction):
    def solver(self, *args):
        _input = args[1].sympify()
        answer = {'sides': [], 'Arith': []}
        for item in _input:
            if str(item[0].args[0].func) == 'distance':
                k = str(item[0].args[0].args[0]) + str(item[0].args[0].args[1])
                answer['sides'].append([k, item[1]])
            elif isinstance(item[0], Abs):
                k = str(item[0].args[0].args[0])
                answer['sides'].append([k, item[1]])
            elif isinstance(item[0], Mul):
                answer['Arith'].append([item[0], item[1]])

        self.output.append(BaseSymbolValue(answer))

        return self
